﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Net.Chirash.Twitter.TwitterClient;
using Net.Chirash.Twitter.TwitterDatabase;

namespace Net.Chirash.Twitter.TweetZone.WindowComponents.ViewModels
{
    public class TwitterListContainerViewModel : Net.Chirash.Twitter.TweetZone.WindowComponents.ViewModels.PagableContainerViewModel<TwitterList>
    {
        #region Constructors

        public TwitterListContainerViewModel(User user)
            :base(user)
        {
            this.TwitterCommand = TwitterCommandFactory.CreateRemoteTwitterCommand();
        }

        public TwitterListContainerViewModel()
            :this(App.CurrentApp.User)
        {
        }

        #endregion

        #region Public Members

        public override string ContainerTitle
        {
            get
            {
                return string.Format("Lists of @{0}", CurrentUser.ScreenName);
            }
        }

        #endregion

        #region Protected Methods

        protected override void UpdateDatabase(TwitterCaching caching, IEnumerable<TwitterList> updatelist)
        {
            //caching.UpdateTwitterListDatabase(updatelist);
        }

        protected override void RefreshDataProviding(RefreshArgs<TwitterList> e)
        {
            var cmd = this.TwitterCommand as TwitterCommand;
            var option = new ListsGetOption()
            {
                cursor = -1,
            };

            var lists = cmd.GetLists(this.CurrentUser, option);
            if (e.CollectionCount == 0)
                this.nextCursor = lists.NextCursor;

            e.ResultEnumerable = lists;
        }

        protected override void PagingDataProviding(PagingEventArgs<TwitterList> e)
        {
            var cmd = this.TwitterCommand as TwitterCommand;
            var option = new ListsGetOption()
            {
                cursor = this.nextCursor,
            };

            var lists = cmd.GetLists(this.CurrentUser, option);
            this.nextCursor = lists.NextCursor;
            e.ResultEnumerable = lists;
        }

        #endregion

        #region Private Methods

        private long nextCursor;

        #endregion
    }
}
